package com.kindness.frate.activity;

import android.os.Bundle;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.kindness.frate.R;
import com.kindness.frate.fragment.ContentFragment;
import com.kindness.frate.fragment.HomeFragment;
import com.kindness.frate.fragment.ScrollFragment;
import com.kindness.frate.widget.SVViewPager;
import com.kindnesslibrary.base.BaseActivity;
import com.kindnesslibrary.tablayout.OnTabSelectListener;
import com.kindnesslibrary.tablayout.SlidingTabLayout;
import com.kindnesslibrary.util.ToastUtil;
import com.kindnesslibrary.widget.ColorTrackView;

import java.util.ArrayList;
import java.util.List;

/**
 * scrollview+viewpager+headerview解决事件冲突等问题
 */
public class PullZoomViewPagerActivity extends BaseActivity {

    SVViewPager vp_pull_zoom_vp;
    //    TabLayout tl_levitate_tab;
    SlidingTabLayout stl_levitate;
    List<Fragment> fragments = new ArrayList<>();
    PullZoomVPAdapter pullZoomVPAdapter;
    private String[] mTitles = new String[]{"社区服务", "传统文化", "萌趣社区", "社区服务", "传统文化", "萌趣社区"};
//    private List<ColorTrackView> mTabs = new ArrayList<ColorTrackView>();

    @Override
    protected void handlerMessage(Message msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_pull_zoom_view_pager_layout;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        vp_pull_zoom_vp = getView(R.id.vp_pull_zoom_vp);
//        tl_levitate_tab = getView(R.id.tl_levitate_tab);
        stl_levitate = getView(R.id.stl_levitate);
        fragments.add(HomeFragment.getInstance());
        fragments.add(ContentFragment.newInstance());
        fragments.add(ScrollFragment.newInstance());
        fragments.add(HomeFragment.getInstance());
        fragments.add(ContentFragment.newInstance());
        fragments.add(ScrollFragment.newInstance());
//        mTabs.add((ColorTrackView) findViewById(R.id.id_tab_01));
//        mTabs.add((ColorTrackView) findViewById(R.id.id_tab_02));
//        mTabs.add((ColorTrackView) findViewById(R.id.id_tab_03));
        pullZoomVPAdapter = new PullZoomVPAdapter(getSupportFragmentManager(), fragments, mTitles);
        vp_pull_zoom_vp.setAdapter(pullZoomVPAdapter);
        stl_levitate.setViewPager(vp_pull_zoom_vp);
        stl_levitate.showDot(4);
        stl_levitate.showMsg(2, 5);
        stl_levitate.setMsgMargin(2, 0, 10);
//        for (String title : mTitles){
//            tl_levitate_tab.addTab(tl_levitate_tab.newTab().setText(title));
//        }
//        tl_levitate_tab.setupWithViewPager(vp_pull_zoom_vp);
    }

    @Override
    protected void initEvent() {
        stl_levitate.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                stl_levitate.hideMsg(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
//        vp_pull_zoom_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                if (positionOffset > 0) {
//                    ColorTrackView left = mTabs.get(position);
//                    ColorTrackView right = mTabs.get(position + 1);
//                    left.setDirection(1);
//                    right.setDirection(0);
//                    left.setProgress(1 - positionOffset);
//                    right.setProgress(positionOffset);
//                }
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
    }

    public void test(View view) {
        ToastUtil.showToast("test");
    }

    class PullZoomVPAdapter extends FragmentPagerAdapter {

        List<Fragment> fragments;
        private String tabTitles[];

        public PullZoomVPAdapter(FragmentManager fm, List<Fragment> fragments, String[] mTitles) {
            super(fm);
            this.fragments = fragments;
            this.tabTitles = new String[mTitles.length];
            System.arraycopy(mTitles, 0, this.tabTitles, 0, mTitles.length);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles[position];
        }
    }

}
